package com.springboot.demo.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author wubc683
 * @desc
 * @date 2021-07-30 15:25
 */
@NoArgsConstructor
@Component
@ApiModel
public class ResponseUtils<T> implements Serializable {
    @ApiModelProperty(value = "错误码")
    private int code;
    @ApiModelProperty(value = "错误消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private T data;

    public ResponseUtils(ResponseResult responseResult, T data) {
        this.code = responseResult.getCode();
        this.message = responseResult.getMessage();
        this.data = data;
    }

    public ResponseUtils(Integer code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseUtils<T> buildSuccessResponse() {
        return new ResponseUtils<>(ResponseResult.SUCCESS, null);
    }

    public ResponseUtils<T> buildSuccessResponse(T data) {
        return new ResponseUtils<>(ResponseResult.SUCCESS, data);
    }

    public ResponseUtils<T> buildFailedResponse(int code, String message, T data) {
        return new ResponseUtils<>(code,message, data);
    }

    public ResponseUtils<T> buildFailedResponse(int code, String message) {
        return new ResponseUtils<>(code, message,null);
    }

    public ResponseUtils<T> buildFailedResponse(ResponseResult responseResult) {
        return new ResponseUtils<>(responseResult,null);
    }

    public ResponseUtils<T> buildFailedResponse(ResponseResult responseResult, T data) {
        return new ResponseUtils<T>(responseResult, data);
    }

    public ResponseUtils<T> buildFailedResponse(Integer code, String message, T data) {
        return new ResponseUtils<T>(code, message, data);
    }

    public ResponseUtils<T> buildResponse(ResponseResult responseResult, T data) {
        return new ResponseUtils<T>(responseResult, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}